﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ROrganizer.Client;
using ROrganizer.Client.Files;
using ROrganizer.UI.Forms;

namespace ROrganizer.UI
{
    public static class ViewManager
    {
        public static Icon ApplicationIcon { get; set; }

        public static HashCredentials ShowCredentials(IWin32Window owner, bool allowCreateAccount = true)
        {
            using (var form = new CredentialsForm(allowCreateAccount) { Icon = ViewManager.ApplicationIcon })
                if (form.ShowDialog(owner) == DialogResult.OK)
                    return form.GetCredentials();

            return null;
        }

        public static bool ShowCreateUser(IWin32Window owner)
        {
            using (var form = new CreateUserForm { Icon = ViewManager.ApplicationIcon })
                return form.ShowDialog(owner) == DialogResult.OK;
        }


        public static bool ShowUpdate(IWin32Window owner)
        {
            using (var form = new UpdateForm { Icon = ViewManager.ApplicationIcon })
                return form.ShowDialog(owner) == DialogResult.OK;
        }

        public static bool ShowCommit(IWin32Window owner)
        {
            using (var form = new CommitForm { Icon = ViewManager.ApplicationIcon })
                return form.ShowDialog(owner) == DialogResult.OK;
        }


        public static bool ShowProposalManager(IWin32Window owner)
        {
            using (var form = new ProposalManagerForm { Icon = ViewManager.ApplicationIcon })
                return form.ShowDialog(owner) == DialogResult.OK;
        }

        public static bool ShowLibraryManager(IWin32Window owner)
        {
            using (var form = new LibraryManagerForm { Icon = ViewManager.ApplicationIcon })
                return form.ShowDialog(owner) == DialogResult.OK;
        }

        public static bool ShowFolderManager(IWin32Window owner)
        {
            using (var form = new FolderManagerForm { Icon = ViewManager.ApplicationIcon })
                return form.ShowDialog(owner) == DialogResult.OK;
        }

        public static void ShowResultsManager(IWin32Window owner)
        {
            using (var form = new CrawlerResultsForm { Icon = ViewManager.ApplicationIcon })
                form.ShowDialog(owner);
        }


        public static bool ShowItemDetails(IWin32Window owner, ImdbEntry[] entries)
        {
            using (var form = new ItemDetailsForm(entries) { Icon = ViewManager.ApplicationIcon })
                return form.ShowDialog(owner) == DialogResult.OK;
        }

        public static bool ShowFileDetails(IWin32Window owner, string path)
        {
            using (var form = new FileDetailsForm(path) { Icon = ViewManager.ApplicationIcon })
                return form.ShowDialog(owner) == DialogResult.OK;
        }


        public static CopyTask[] ShowCopySettings(IWin32Window owner, string[] files)
        {
            using (var form = new CopySettingsForm(files) { Icon = ViewManager.ApplicationIcon })
                if (form.ShowDialog(owner) == DialogResult.OK)
                    return form.GetTasks();

            return null;
        }

        public static RemoveTask[] ShowRemoveSettings(IWin32Window owner, string[] files)
        {
            using (var form = new RemoveSettingsForm(files) { Icon = ViewManager.ApplicationIcon })
                if (form.ShowDialog(owner) == DialogResult.OK)
                    return form.GetTasks();

            return null;
        }

        public static void ShowCrawlerSettings(IWin32Window owner)
        {
            using (var form = new CrawlerSettingsForm { Icon = ViewManager.ApplicationIcon })
                form.ShowDialog(owner);
        }


        public static bool ShowProgress(IWin32Window owner, CopyTask[] tasks)
        {
            using (var form = new ProgressForm(tasks) { Icon = ViewManager.ApplicationIcon })
                return form.ShowDialog(owner) == DialogResult.OK;
        }

        public static bool ShowProgress(IWin32Window owner, RemoveTask[] tasks)
        {
            using (var form = new ProgressForm(tasks) { Icon = ViewManager.ApplicationIcon })
                return form.ShowDialog(owner) == DialogResult.OK;
        }


        public static bool ShowAbout(IWin32Window owner)
        {
            using (var form = new AboutForm { Icon = ViewManager.ApplicationIcon })
                return form.ShowDialog(owner) == DialogResult.OK;
        }

        public static bool ShowFeedback(IWin32Window owner)
        {
            using (var form = new FeedbackForm { Icon = ViewManager.ApplicationIcon })
                return form.ShowDialog(owner) == DialogResult.OK;
        }


        public static void RunROrganizer()
        {
            Application.Run(new ROrganizerForm { Icon = ViewManager.ApplicationIcon });
        }

        public static void RunCrawler()
        {
            Application.Run(new CrawlerForm { Icon = ViewManager.ApplicationIcon });
        }


        public static string ChooseFolder(IWin32Window owner, string description, string path = null)
        {
            using (var form = new FolderBrowserDialog())
            {
                form.Description = description;
                form.ShowNewFolderButton = true;

                if (!String.IsNullOrEmpty(path))
                    form.SelectedPath = path;

                if (form.ShowDialog(owner) == DialogResult.OK)
                    return form.SelectedPath;

                return null;
            }
        }
    }
}
